package top.zlhy7.module.work.vo;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import top.zlhy7.enums.OrderTypeEnum;

import java.math.BigDecimal;
import java.util.Optional;

import static top.zlhy7.base.constant.Constants.ONE_HUNDRED;

/**
 * @author renyong
 * @date 2024/9/30 01:08
 * @description 工单统计3：查询7月每个工单类型的工单总量、超期率。
 */
@Data
public class WorkOrderStatistics3Vo {
    /**
     * 工单类型0交办1直接答复3无效工单
     */
    private Integer orderType;
    /**
     * 工单类型名称
     */
    private String orderTypeStr;
    /**
     * 总工单数
     */
    private Integer totalOrderQuantity;
    /**
     * 超期工单数
     */
    private Integer overdueOrderQuantity;
    /**
     * 超期率=(超期工单数/总工单数)*100%
     */
    private BigDecimal overdueRate;
    /**
     * 超期率 百分数形式
     */
    private String overdueRatePerCent;
    //region 筛选条件
    /**
     * 统计月份，格式：yyyy-MM
     */
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String yearMonth;
    //endregion

    public WorkOrderStatistics3Vo() {
        this.totalOrderQuantity = 0;
        this.overdueOrderQuantity = 0;
        this.overdueRate = BigDecimal.ZERO;
        this.overdueRatePerCent = "0%";
    }
    public WorkOrderStatistics3Vo(Integer orderType){
        this();
        this.setOrderType(orderType);
    }
    public void setOverdueRate(BigDecimal overdueRate){
        this.overdueRate = overdueRate;
        if (overdueRate==null) {
            this.overdueRatePerCent = "0%";
            return;
        }
        this.overdueRatePerCent = overdueRate.multiply(ONE_HUNDRED).setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "%";
    }

    public void setOrderType(Integer orderType) {
        this.orderType = orderType;
        if (orderType==null){
            return;
        }
        Optional.of(orderType)
                .map(OrderTypeEnum.orderTypeEnumMap::get)
                .map(OrderTypeEnum::getDescription)
                .ifPresent(this::setOrderTypeStr);
    }
}
